package com.block.channel.service.monitor;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.channel.enums.MonitorChannelEnum;
import com.block.channel.model.dto.MonitorInfoDTO;
import com.block.channel.model.result.CallbackResult;
import com.block.channel.service.IChannelCallbackService;
import com.block.channel.service.IChannelMonitorService;
import com.block.common.service.LogFactory;
import com.block.common.util.LocalDateUtil;
import com.block.db.entity.ChannelBaseInfo;
import com.block.db.entity.SystemConstantInfo;
import com.block.db.mapper.SystemConstantInfoMapper;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;


/**
 * http://baidu.com/click?source= ×××&adid= × × ×
 *
 * &idfa={adinall_idfa}&ip ={adinall_ip}&ua ={ua}&appname ={adinall_appname}
 * &call back ={adinall_callback}&clickid ={adinall_clickid}&imei={adinall_imei}
 * &imeiMd5={adinall_imeiMd5}&oaid={adin all_oaid}&oaidMd5 ={adinall_oaidMd5}
 */
@Service
public class JuGaoMonitorService implements IChannelMonitorService, IChannelCallbackService {

    private static Logger log = LogFactory.getCommLog();

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public MonitorInfoDTO handleRequestParam(ChannelBaseInfo channelBaseInfo, String actionType, Map<String, String[]> parameterMap, String paramContent) {

        MonitorInfoDTO monitorInfoDTO = new MonitorInfoDTO();

        monitorInfoDTO.setPlatform(MonitorChannelEnum.JUGAO.getValue());

        monitorInfoDTO.setChannelCode(channelBaseInfo.getChannelCode());
        monitorInfoDTO.setChannelName(channelBaseInfo.getChannelName());
        monitorInfoDTO.setMonitorInfo(JSON.toJSONString(parameterMap));

        String[] idfaArr = parameterMap.get("idfa");
        String[] ipArr = parameterMap.get("ip");
        String[] uaArr = parameterMap.get("ua");
        String[] appnameArr = parameterMap.get("appname");
        String[] callbackArr = parameterMap.get("callback");
        String[] clickidArr = parameterMap.get("clickid");
        String[] imeiArr = parameterMap.get("imei");
        String[] imeiMD5Arr = parameterMap.get("imeiMD5");
        String[] oaidArr = parameterMap.get("oaid");
        String[] oaidMd5Arr = parameterMap.get("oaidMd5");

        if (ObjectUtil.isNotEmpty(clickidArr)){
            monitorInfoDTO.setUniqueKey(clickidArr[0]);
            monitorInfoDTO.setKeyType("ID");
        }

        JSONObject callbackObject = new JSONObject();
//        if (ObjectUtil.isNotEmpty(callbackArr)){
//            callbackObject.put("requestId",REQUESTIDArr[0]);
//        }

        monitorInfoDTO.setCallbackInfo(JSON.toJSONString(callbackObject));
        monitorInfoDTO.setActionType(actionType);

        return monitorInfoDTO;
    }

    @Override
    public CallbackResult handleCallback(ChannelBaseInfo channelBaseInfo, String function, Map<String, String[]> parameterMap, String paramContent) {
        return null;
    }


}
